#include <iostream>
using namespace std;

//【题目272】编写-个使用类模板对数组进行排序、查找和求元素和的程序。

template <typename T>
class Array
{
public:
    Array(int n)
    {
        this->n = n;
        data = new T[n];
    }
    ~Array()
    {
        delete[] data;
    }

    T &operator[](int i)
    {
        return data[i];
    }

    void sort()
    {
        for (int i = 0; i < n - 1; i++)
        {
            for (int j = 0; j < n - i - 1; j++)
            {
                if (data[j] > data[j + 1])
                {
                    T temp = data[j];
                    data[j] = data[j + 1];
                    data[j + 1] = temp;
                }
            }
        }
    }

    int find(T key)
    {
        for (int i = 0; i < n; i++)
        {
            if (data[i] == key)
            {
                return i;
            }
        }
        return -1;
    }

    void sum()
    {
        int s = 0;
        for (int i = 0; i < n; i++)
        {
            s += data[i];
        }
        cout << s << endl;
    }

    void print()
    {
        for (int i = 0; i < n; i++)
        {
            cout << data[i] << " ";
        }
        cout << endl;
    }

private:
    T *data;
    int n;
};

int main()
{
    Array<int> arr(5);
    for (int i = 0; i < 5; i++)
    {
        arr[i] = i + 1;
    }
    arr[3] = 6;
    arr.print();
    arr.sort();
    arr.print();
    arr.sum();
    cout << arr.find(3) << endl;
    return 0;
}